﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using ThousandFunClient.model;
using System.Reflection;
using System.Windows.Forms;
using System.Collections;

namespace ThousandFunClient.Util
{
    class ConfigUtil
    {
        //设置记录到xml文件
        public static void xmlUpdate()
        {
            if (File.Exists(TFConfig.ConfigXMLAddress)) //文件存在就删除
            {
                FileInfo file = new FileInfo(TFConfig.ConfigXMLAddress);
                file.Delete();
            }

            XElement config = new XElement("ThousandFunConfig");

                XElement el = new XElement("PortA", 
                       new XElement("PortName", TFConfig.GetInstance().PortConfigA.PortName),
                       new XElement("Handshake", TFConfig.GetInstance().PortConfigA.Handshake),
                       new XElement("StopBits", TFConfig.GetInstance().PortConfigA.StopBits),
                       new XElement("DataBits", TFConfig.GetInstance().PortConfigA.DataBits),
                       new XElement("BaudRate", TFConfig.GetInstance().PortConfigA.BaudRate),
                       new XElement("Parity", TFConfig.GetInstance().PortConfigA.Parity),
                       new XElement("ReadTimeout", TFConfig.GetInstance().PortConfigA.ReadTimeout)
                       );
                config.Add(el);

                XElement elb = new XElement("PortB",
                       new XElement("PortName", TFConfig.GetInstance().PortConfigB.PortName),
                       new XElement("Handshake", TFConfig.GetInstance().PortConfigB.Handshake),
                       new XElement("StopBits", TFConfig.GetInstance().PortConfigB.StopBits),
                       new XElement("DataBits", TFConfig.GetInstance().PortConfigB.DataBits),
                       new XElement("BaudRate", TFConfig.GetInstance().PortConfigB.BaudRate),
                       new XElement("Parity", TFConfig.GetInstance().PortConfigB.Parity),
                       new XElement("ReadTimeout", TFConfig.GetInstance().PortConfigB.ReadTimeout)
                       );

                config.Add(elb);

                XElement elbc = new XElement("TorqueConfig",
                           new XElement("CMD", TFConfig.GetInstance().TorqueConfig.SendCMD),
                           new XElement("Interval", TFConfig.GetInstance().TorqueConfig.Interval),
                           new XElement("LoggerName", TFConfig.GetInstance().TorqueConfig.LoggerName),
                           new XElement("ReadTimeout", TFConfig.GetInstance().TorqueConfig.ReadTimeout)
                           );
                config.Add(elbc);

                XElement elbcg = new XElement("AngleConfig",
                          new XElement("CMD", TFConfig.GetInstance().AngleConfig.SendCMD),
                           new XElement("Interval", TFConfig.GetInstance().AngleConfig.Interval),
                           new XElement("LoggerName", TFConfig.GetInstance().AngleConfig.LoggerName),
                           new XElement("ReadTimeout", TFConfig.GetInstance().AngleConfig.ReadTimeout)
               );


                config.Add(elbcg);

                Hashtable ht = TFConfig.GetInstance().HtBatchCMD;

                XElement eCMD = new XElement("BatchCMD");

                foreach (DictionaryEntry de in ht) //ht为一个Hashtable实例
                {
                    SendCMDConfig sc = (SendCMDConfig)de.Value;
                    XElement eSendCMD = new XElement("SendCMD",
                     new XElement("CMD", sc.SendCMD),
                       new XElement("Interval", sc.Interval),
                       new XElement("LoggerName", sc.LoggerName)
                       );
                    eCMD.Add(eSendCMD);
                }
                

                config.Add(eCMD);
                config.Save(TFConfig.ConfigXMLAddress);
            }


        //建立xml设置文件
        public static void initConfig()
        {
            if (!File.Exists(TFConfig.ConfigXMLAddress)) //文件不存在就新建
            {
                XElement config = new XElement("ThousandFunConfig");

                XElement el = new XElement("PortA", 
                       new XElement("PortName", "com7"),
                       new XElement("Handshake", ""),
                       new XElement("StopBits", ""),
                       new XElement("DataBits", ""),
                       new XElement("BaudRate", "57600"),
                       new XElement("Parity", ""),
                       new XElement("ReadTimeout", "2000")
                       );
                config.Add(el);

                XElement elb = new XElement("PortB",
                       new XElement("PortName", "com7"),
                       new XElement("Handshake", ""),
                       new XElement("StopBits", ""),
                       new XElement("DataBits", ""),
                       new XElement("BaudRate", "57600"),
                       new XElement("Parity", ""),
                       new XElement("ReadTimeout", "2000")
                       );

                config.Add(elb);

                
                XElement elbcy = new XElement("CycleNumberConfig",
                       new XElement("CMD", "00 06 02 01 0B 00 40 43 68"),
                       new XElement("Interval", "1000"),
                       new XElement("LoggerName", "ThousandFun.angle"),
                       new XElement("ReadTimeout", "2000")
                       );
                config.Add(elbcy);


                XElement elbc = new XElement("TorqueConfig",
                       new XElement("CMD", "00 06 02 01 0B 00 40 10 9B"),
                       new XElement("Interval", "1000"),
                       new XElement("LoggerName", "ThousandFun.angle"),
                       new XElement("ReadTimeout", "2000")
                       );
                config.Add(elbc);

                XElement elbcg = new XElement("AngleConfig",
               new XElement("CMD", "00 06 02 01 0B 00 40 20 8B"),
               new XElement("Interval", "1000"),
               new XElement("LoggerName", "ThousandFun.angle"),
               new XElement("ReadTimeout", "2000")
               );


                config.Add(elbcg);


                XElement eSendCMD = new XElement("SendCMD",
                     new XElement("CMD", "00 06 02 01 0B 00 40 43 68"),
                       new XElement("Interval", "1000"),
                       new XElement("LoggerName", "ThousandFun.angle")
                       );

                XElement eSendCMD1 = new XElement("SendCMD",
                     new XElement("CMD", "00 06 02 01 0B 00 40 40 6B"),
                       new XElement("Interval", "2000"),
                       new XElement("LoggerName", "ThousandFun.angle")
                       );

                XElement eSendCMD2 = new XElement("SendCMD",
                     new XElement("CMD", "00 06 02 01 0B 00 40 41 6A"),
                       new XElement("Interval", "3000"),
                       new XElement("LoggerName", "ThousandFun.angle")
                       );
                
                XElement eSendCMD3 = new XElement("SendCMD",
                 new XElement("CMD", "00 06 02 01 0B 00 40 42 69"),
               new XElement("Interval", "4000"),
               new XElement("LoggerName", "ThousandFun.angle")
             );

                XElement eCMD = new XElement("BatchCMD",
                        eSendCMD,
                       eSendCMD1,
                       eSendCMD2,
                       eSendCMD3
                       );


                
                config.Add(eCMD);


                config.Save(TFConfig.ConfigXMLAddress);
            }


            
        }

       

    }
}
